<template>
  <a-modal
    title="新增数据库信息表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="数据库内表id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入数据库内表id" v-decorator="['tableId', {rules: [{required: true, message: '请输入数据库内表id！'}]}]" />
        </a-form-item>
        <a-form-item
          label="指标名称id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入指标名称id" v-decorator="['indicatorsNameId', {rules: [{required: true, message: '请输入指标名称id！'}]}]" />
        </a-form-item>
        <a-form-item
          label="主数据指标id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入主数据指标id" v-decorator="['masterDataId', {rules: [{required: true, message: '请输入主数据指标id！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段权限分级"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段权限分级" v-decorator="['columnPermissions', {rules: [{required: true, message: '请输入字段权限分级！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段名" v-decorator="['columnName', {rules: [{required: true, message: '请输入字段名！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段备注" v-decorator="['cloumnRemarks', {rules: [{required: true, message: '请输入字段备注！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段类型" v-decorator="['cloumnType', {rules: [{required: true, message: '请输入字段类型！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段长度"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段长度" v-decorator="['cloumnLength', {rules: [{required: true, message: '请输入字段长度！'}]}]" />
        </a-form-item>
        <a-form-item
          label="索引"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入索引" v-decorator="['cloumnIndex', {rules: [{required: true, message: '请输入索引！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段所在的Catalog名字"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段所在的Catalog名字" v-decorator="['cloumnCatalogName', {rules: [{required: true, message: '请输入字段所在的Catalog名字！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段小数点后的位数"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段小数点后的位数" v-decorator="['cloumnScale', {rules: [{required: true, message: '请输入字段小数点后的位数！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段是否自动递增"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段是否自动递增" v-decorator="['cloumnIsAuto', {rules: [{required: true, message: '请输入字段是否自动递增！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段是否是主键"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段是否是主键" v-decorator="['cloumnIsPrimary', {rules: [{required: true, message: '请输入字段是否是主键！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段在数据库中是否为货币型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段在数据库中是否为货币型" v-decorator="['cloumnIsCurrency', {rules: [{required: true, message: '请输入字段在数据库中是否为货币型！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段是否为空"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段是否为空" v-decorator="['cloumnIsNullable', {rules: [{required: true, message: '请输入字段是否为空！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段是否为只读"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段是否为只读" v-decorator="['cloumnIsReadonly', {rules: [{required: true, message: '请输入字段是否为只读！'}]}]" />
        </a-form-item>
        <a-form-item
          label="字段是否出现在where中"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入字段是否出现在where中" v-decorator="['cloumnIsSearchable', {rules: [{required: true, message: '请输入字段是否出现在where中！'}]}]" />
        </a-form-item>
        <a-form-item
          label="访问权限"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入访问权限" v-decorator="['permissions', {rules: [{required: true, message: '请输入访问权限！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { tableFieldAdd } from '@/api/modular/main/tablefield/tableFieldManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            tableFieldAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
